package cn.com.bdqn.controller;

import cn.com.bdqn.entity.SalChance;
import cn.com.bdqn.entity.SysUser;
import cn.com.bdqn.service.SysUserService;
import cn.com.bdqn.utils.JwtUtils;
import cn.com.bdqn.utils.ReturnData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户管理接口")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Operation(summary = "登录方法")
    @PostMapping("/login")
    public ReturnData login(SysUser sysUser){
        //  使用sysUserService的login方法进行登录
        SysUser  login  =  sysUserService.login(sysUser);
        //  如果登录成功且获取到的用户ID不为0
        if  (login  !=  null  &&  login.getUsrId()  !=  0){
            //  生成jwt令牌
            String  jwt  =  JwtUtils.generateToken(login);
            //  打印jwt令牌
            System.out.println(jwt);
            //  返回成功结果，其中包括登录用户和jwt令牌
            return  ReturnData.ok().put("sysUser",login).put("jwt",jwt);
        }  else  {
            //  登录失败，返回错误信息
            return  ReturnData.currError("账号密码错误");
        }

    }

    @Operation(summary = "根据jwt字符串解码获取用户信息")
    @PostMapping("/jwtDecode")
    public ReturnData jwtDecode(String jwt){
        //  解析JWT获取其中的Claims
        Claims  claims  =  JwtUtils.verifyJwt(jwt);
        //  构建包含Claims信息的返回数据
        return  ReturnData.ok().put("claims",claims);
    }

    @Operation(summary = "查询所有客户经理的方法")
    @PostMapping("/queryManger")
    public ReturnData queryManger(){
        try {
            List<SysUser> sysUsers = sysUserService.queryManger();
            return ReturnData.ok().put("manger",sysUsers);
        }catch (Exception e){
            return ReturnData.error();
        }
    }

    @Operation(summary = "根据用户ID查询用户方法")
    @PostMapping("/queryById")
    public ReturnData queryById(SysUser sysUser){
        try{
            SysUser sysUser1 = sysUserService.queryById(sysUser);
            return ReturnData.ok().put("userById",sysUser1);
        }catch (Exception e){
            e.printStackTrace();
            return ReturnData.currError("根据用户ID查询用户方法异常");
        }
    }


    @Operation(summary = "用户管理高级查询")
    @PostMapping("/queryByLike")
    public ReturnData queryByLike(SysUser sysUser, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "15") int pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Map> mapList = sysUserService.queryByLike(sysUser);
            PageInfo pageInfo = new PageInfo(mapList,3);
            return ReturnData.ok().put("mapList",mapList).put("pageInfo",pageInfo);
        }catch (Exception e){
            return ReturnData.currError("用户管理高级查询异常");
        }
    }

    @Operation(summary = "修改用户信息")
    @PostMapping("/updateSysUser")
    public ReturnData updateSysUser(SysUser sysUser){
        int result = sysUserService.updateSysUser(sysUser);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("修改用户信息异常");
        }
    }

    @Operation(summary = "删除用户信息")
    @PostMapping("/delSysUser")
    public ReturnData delSysUser(Long usrId){
        int result = sysUserService.delSysUser(usrId);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("删除用户信息异常");
        }
    }

    @Operation(summary = "新增用户信息")
    @PostMapping("/addSysUser")
    public ReturnData addSysUser(SysUser sysUser){
        int result = sysUserService.addSysUser(sysUser);
        if (result > 0){
            return ReturnData.ok();
        }else {
            return ReturnData.currError("新增用户信息异常");
        }
    }

}
